<?php

namespace App\Http\Controllers\Api;

use App\Coupons;
use App\Http\Services\CommonManager;
use App\Http\Services\UserManager;
use App\User;
use App\UserCoupon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CouponController extends Controller
{
    /**
     * 优惠券列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCouponList(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);

        $limit = !is_null($request->get('limit')) ? $request->get('limit') : 20;
        $page  = !is_null($request->get('page')) ? $request->get('page') : 1;

        $pageInfo = [
            'limit' => $limit,
            'page'  => $page
        ];

        /** @var Coupons $coupon */
        $coupons = Coupons::whereStatus(1);

        $pageInfo['total'] = $coupons->count();

        $couponIds = $coupons->orderBy('created_at','desc')->offset($limit * ($page - 1))->limit($limit)->pluck('id')->toArray();

        $couponList = CommonManager::getCouponToId($couponIds,$user);

        return response()->json(['level' => 'success','message' => '获取成功','data' => $couponList,'page_info' => $pageInfo]);
    }

    /**
     * 优惠券详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCouponDetail(Request $request){
        $coupon_id=!is_null($request->get('coupon_id'))?$request->get('coupon_id'):0;

        $user = UserManager::isLogin($request);

        $coupon = CommonManager::getCouponToId($coupon_id,$user);

        return response()->json(['level' => 'success','message' => '获取成功','data' => $coupon]);
    }

    /**
     * 优惠券领取入口
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function couponReceive(Request $request){
        /** @var User $user */
        $user = UserManager::isLogin($request);
        if(!$user)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $couponId = !is_null($request->get('coupon_id')) ? $request->get('coupon_id') : 0;

        /** @var Coupons $thisCoupon */
        $thisCoupon = Coupons::whereId($couponId)->first();
        if(!$thisCoupon)
            return response()->json(['level' => 'error','message' => '该优惠券不存在！']);

        if($thisCoupon->status == 0)
            return response()->json(['level' => 'error','message' => '系统错误']);

        if(strtotime($thisCoupon->end_time) < time())
            return response()->json(['level' => 'error','message' => '优惠券已过期']);

        $userCoupon = UserCoupon::whereUserId($user->id)->whereCouponId($thisCoupon->id)->first();
        if($userCoupon)
            return response()->json(['level' => 'error','message' => '您已经领取过了']);

        if($thisCoupon->num==0)
            return response()->json(['level' => 'error','message' => '优惠券已经领取完，下次再来']);
        $thisCoupon->num=$thisCoupon->num-1;
        $thisCoupon->save();

        $newUserCoupon = new UserCoupon();
        $newUserCoupon->user_id       = $user->id;
        $newUserCoupon->coupon_id     = $thisCoupon->id;
        $newUserCoupon->name          = $thisCoupon->name;
        $newUserCoupon->money         = $thisCoupon->money;
        $newUserCoupon->type          = $thisCoupon->type;
        $newUserCoupon->satisfy_money = $thisCoupon->satisfy_money;
        $newUserCoupon->start_time    = $thisCoupon->start_time;
        $newUserCoupon->end_time      = $thisCoupon->end_time;
        $newUserCoupon->save();

        return response()->json(['level' => 'success','message' => '领取成功','data' => $newUserCoupon]);
    }
}
